//
//  DetailViewController.swift
//  Swift_base_grammar
//
//  Created by lantaiyuan on 2017/9/14.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController {
    //数据模型
    var persons : Person?
    //定义闭包属性
    var didSaveedCallBack:(() ->())?
    
    @IBOutlet weak var agetextField: UITextField!
    @IBOutlet weak var nameTextField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "个人信息"
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "保存", style: .plain, target: self, action: #selector(rightItemAction))
        nameTextField.text = persons?.name
        agetextField.text = "\(persons?.age ?? 0)"
    }
    //文本框变化
    @IBAction func textChange(_ sender: UITextField) {
       //当两个输入框都有内容的时候才允许保存
      navigationItem.rightBarButtonItem?.isEnabled = nameTextField.hasText && agetextField.hasText
    }
    @objc private func rightItemAction() {
        persons?.name = nameTextField.text
        persons?.age = Int(agetextField.text!) ?? 0
        didSaveedCallBack!()
        navigationController?.popViewController(animated: true)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
